#!/usr/bin/python
# -*- coding: UTF-8 -*-
from pypinyin import lazy_pinyin
import time
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import config.limit_time as limit_time

#获取汉字拼音首字母
def getInitial(strr):
    pinyin = lazy_pinyin(strr)
    initial = ''
    for item in pinyin:
         initial+= item[0]
    
    return initial


def getDate(t = 0):
    timeType = "%Y-%m-%d %H:%M:%S"
    if t ==1: timeType = "%Y-%m-%d 00:00:00"
    return time.strftime(timeType, time.localtime())

def sendEmail(content):
    # 第三方 SMTP 服务
    mail_host="smtp.qq.com"  #设置服务器
    mail_user="240632696@qq.com"    #用户名 mtmvxbkjuznrbgbe
    mail_pass="mtmvxbkjuznrbgbe"   #口令 
    
    
    sender = '240632696@qq.com'
    receivers = ['dong2406@126.com']  # 接收邮件，可设置为你的QQ邮箱或者其他邮箱
    
    message = MIMEText(content, 'plain', 'utf-8')
    #message['From'] = "可转债数据处理<"+mail_user+">"
    message['To'] =  mail_user
    
    subject = '结果报告'
    message['Subject'] = Header(subject, 'utf-8')
    
    
    try:
        smtpObj = smtplib.SMTP() 
        smtpObj.connect(mail_host, 587)    # 25 为 SMTP 端口号
        smtpObj.login(mail_user,mail_pass)  
        smtpObj.sendmail(sender, receivers, message.as_string())
        print("邮件发送成功")
    except smtplib.SMTPException:
        print("Error: 无法发送邮件")

#返回真不采集
def noColl():
    t = time.localtime()
    wday = t.tm_wday
    currDate = time.strftime("%Y-%m-%d", t)

    no_coll = False
    #周六日不采集 返回 true
    if wday > 4:
        no_coll = True
    
    limitDate = limit_time.date
    
    #在限制时间的不采集 返回真
    for i in range(len(limitDate)):
        if limitDate[i] == currDate:
            no_coll = True
            break
            

    return no_coll    
